package com.liuqi.business.controller.admin;


import com.google.common.base.Preconditions;
import com.liuqi.base.BaseAdminController;
import com.liuqi.base.BaseService;
import com.liuqi.business.enums.ProtocolEnum;
import com.liuqi.business.enums.SwitchEnum;
import com.liuqi.business.model.CurrencyExtractModelDto;
import com.liuqi.business.model.CurrencyModelDto;
import com.liuqi.business.model.CurrencyRechargeModel;
import com.liuqi.business.model.CurrencyRechargeModelDto;
import com.liuqi.business.service.CurrencyRechargeService;
import com.liuqi.business.service.CurrencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/admin/currencyRecharge")
public class AdminCurrencyRechargeController extends BaseAdminController<CurrencyRechargeModel,CurrencyRechargeModelDto> {

    @Autowired
    private CurrencyRechargeService currencyRechargeService;
    //jsp基础路径
    private final static String JSP_BASE_PTH="admin";
    //模块
    private final static String BASE_MODUEL="currencyRecharge";
    //默认为""表示可以使用add和update。  重写add或update时，原有方法禁止使用 NOT_OPERATE="add,update"
    private final static String NOT_OPERATE="";
    @Override
    public BaseService getBaseService() {
    	return this.currencyRechargeService;
    }

    @Override
    public String getJspBasePath() {
        return JSP_BASE_PTH;
    }
    @Override
    public String getBaseModuel() {
        return BASE_MODUEL;
    }
    @Override
    public String getNotOperate() { return NOT_OPERATE;}
    @Override
    public String getDefaultExportName() { return DEFAULT_EXPORTNAME;}
    /*******待修改  排序  导出**********************************************************************************************************/
    //默认导出名称
    private final static String DEFAULT_EXPORTNAME="充值配置";
    @Override
    public String[] getDefaultExportHeaders(){
        String[] headers = {"主键","创建时间","更新时间","备注","版本号","币种","协议","充提名称","充值开关（0关 1开）","充值最小数量","充值地址","排序"};
        return headers;
    }
    @Override
    public String[] getDefaultExportColumns(){
        String[] columns = {"id","createTime","updateTime","remark","version","currencyId","protocol","thirdCurrency","rechargeSwitch","minQuantity","address","position"};
        return columns;
    }
    /*******自己代码**********************************************************************************************************/
    @Override
    protected String getNotIncludeField() {
        return super.getNotIncludeField()+",currency_id,protocol,";
    }
    @Autowired
    private CurrencyService currencyService;

    @Override
    protected void toListHandle(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        super.toListHandle(modelMap, request, response);
        this.getEnumList(modelMap);
    }

    @Override
    protected void toAddHandle(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        super.toAddHandle(modelMap, request, response);
        this.getEnumList(modelMap);
    }

    @Override
    protected void toUpdateHandle(CurrencyRechargeModelDto t, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        super.toUpdateHandle(t, modelMap, request, response);
        this.getEnumList(modelMap);
        t.setCurrencyName(currencyService.getNameById(t.getCurrencyId()));
        t.setProtocolStr(ProtocolEnum.getName(t.getProtocol()));
    }
    @Override
    protected void addHandle(CurrencyRechargeModelDto t, Long curUserId, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        CurrencyRechargeModelDto temp=currencyRechargeService.getByCurrencyAndProtocol(t.getCurrencyId(),t.getProtocol());
        Preconditions.checkArgument(temp==null,"已存在数据");
    }

    @Override
    protected void toViewHandle(CurrencyRechargeModelDto t,ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        super.toViewHandle(t,modelMap, request, response);
        this.getEnumList(modelMap);
    }

    private void getEnumList(ModelMap modelMap){
        List<CurrencyModelDto> list= currencyService.getAll();
        modelMap.put("currencyList",list);
        modelMap.put("switchList", SwitchEnum.getList());
        modelMap.put("protocolList", ProtocolEnum.getList());
    }

}
